/*-------------------------------------------------
 *
 * OrbitsWriter - An Offline Blog Writer
 *
 * Copyright (C) 2016  devbean@galaxyworld.org
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 *-------------------------------------------------*/

#include "visualeditor.h"

#include <QApplication>
#include <QDebug>

using namespace Core;
using namespace Core::Internal;

VisualEditorPrivate::VisualEditorPrivate(VisualEditor *ve)
    : q(ve)
{
    connect(q, &QTextEdit::currentCharFormatChanged, this, &VisualEditorPrivate::currentCharFormatChanged);
    connect(q, &QTextEdit::cursorPositionChanged, this, &VisualEditorPrivate::cursorPositionChanged);
}

void VisualEditorPrivate::mergeFormatOnWordOrSelection(const QTextCharFormat &format)
{
    QTextCursor cursor = q->textCursor();
    if (!cursor.hasSelection()) {
        cursor.select(QTextCursor::WordUnderCursor);
    }
    cursor.mergeCharFormat(format);
    q->mergeCurrentCharFormat(format);
}

void VisualEditorPrivate::currentCharFormatChanged(const QTextCharFormat &format)
{
    fontChanged(format.font());
    colorChanged(format.foreground().color());
}

void VisualEditorPrivate::cursorPositionChanged()
{
    alignmentChanged(q->alignment());
}

void VisualEditorPrivate::fontChanged(const QFont &font)
{
    emit q->fontChanged(font);
}

void VisualEditorPrivate::colorChanged(const QColor &color)
{
    emit q->colorChanged(color);
}

void VisualEditorPrivate::alignmentChanged(Qt::Alignment a)
{
    emit q->alignmentChanged(a);
}

//---------- VisualEditor ----------//

VisualEditor::VisualEditor(QWidget *parent)
    : QTextEdit(parent),
      d(new Internal::VisualEditorPrivate(this))
{
    QFont defaultFont = QApplication::font();
    defaultFont.setPointSize(12);
    setFont(defaultFont);
    setStyleSheet("border: 0");
}

VisualEditor::~VisualEditor()
{
    delete d;
}

void VisualEditor::setTextBold(bool setBold)
{
    QTextCharFormat fmt;
    fmt.setFontWeight(setBold ? QFont::Bold : QFont::Normal);
    d->mergeFormatOnWordOrSelection(fmt);
}

void VisualEditor::setTextItalic(bool setItalic)
{
    QTextCharFormat fmt;
    fmt.setFontItalic(setItalic);
    d->mergeFormatOnWordOrSelection(fmt);
}

void VisualEditor::setTextUnderline(bool setUnderline)
{
    QTextCharFormat fmt;
    fmt.setFontUnderline(setUnderline);
    d->mergeFormatOnWordOrSelection(fmt);
}

void VisualEditor::setTextStrikeOut(bool setStrikeOut)
{
    QTextCharFormat fmt;
    fmt.setFontStrikeOut(setStrikeOut);
    d->mergeFormatOnWordOrSelection(fmt);
}
